package com.yoomiito.app.ui.my.remain

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.gyf.barlibrary.ImmersionBar
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.my.MyRemainMoneyInfo
import com.yoomiito.app.ui.my.psw.SetPayPswActivity
import com.yoomiito.app.ui.my.remain.transfer.TransferActivity
import com.yoomiito.app.utils.AllUtils
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.utils.UserInfoConfig
import com.yoomiito.app.widget.TipDialog

import java.util.concurrent.TimeUnit

import com.jakewharton.rxbinding3.view.clicks
import com.yoomiito.app.adapter.PendingOrderAdapter
import com.yoomiito.app.adapter.my.RemainAdapter
import com.yoomiito.app.model.bean.OrderList
import com.yoomiito.app.model.my.ApplyInfo
import com.yoomiito.app.ui.my.remain.detail.MyMoneyDetailActivity
import com.yoomiito.app.ui.my.remain.withdraw.WithdrawListActivity
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.widget.SpacingDecoration
import kotlinx.android.synthetic.main.act_deduct_money.smartRefreshLayout
import kotlinx.android.synthetic.main.activity_my_remain.*
import kotlinx.android.synthetic.main.title_bar.iv_back_left
import kotlinx.android.synthetic.main.title_bar.tv_center
import kotlinx.android.synthetic.main.view_my_order_top_remain.*
import kotlinx.android.synthetic.main.view_star_coin.*

class PendingOrderActivity : BaseActivity<PendingOrderPresenter>() {
    private var mRemainMoney: String? = null
    private var type = ACTIVITY_TYPE_REMAIN
    private var mStarCoin: View? = null
    private var mAdapter: PendingOrderAdapter? = null
    private var page: Int = 1
    var empty: View? = null

    companion object {
        const val REMAIN_MONEY_KEY = "remain_money_key"
        const val ACTIVITY_TYPE_REMAIN = 1
        const val KEY_ACTIVITY_TYPE = "key_activity_type"
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_pending_order
    }

    override fun newP(): PendingOrderPresenter {
        return PendingOrderPresenter(App.mAppConfig)
    }

    override fun onStart() {
        super.onStart()
    }

    override fun initData(savedInstanceState: Bundle?) {
        tv_center.setText("待计算订单")
        type = intent.getIntExtra(KEY_ACTIVITY_TYPE, ACTIVITY_TYPE_REMAIN)
        empty = LayoutInflater.from(context).inflate(R.layout.item_live_empty, null, false)
        val text = empty?.findViewById<TextView>(R.id.item_empty_tv)
        text?.text = ""
        mRemainMoney = intent.getStringExtra(REMAIN_MONEY_KEY)
        initTop()
        mAdapter = PendingOrderAdapter()
        rcy.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        rcy.addItemDecoration(SpacingDecoration(0, DensityUtil.dp2px(1f), true))
        rcy.adapter = mAdapter
        smartRefreshLayout.setOnRefreshListener {
            page = 1;
            p.settledOrder(page);
        }
        smartRefreshLayout.setOnLoadMoreListener {
            page++
            p.settledOrder(page);
        }
        mAdapter?.setOnItemClickListener { adapter, view, position ->

        }
        p.settledOrder(page);


    }


    private fun initTop() {
        iv_back_left.setOnClickListener { finish() }
        tv_money.typeface = AllUtils.getTypeface()
//        tv_money.text = mRemainMoney
    }


    //设置金额
    private fun setRemainMoney(money: String) {
        dismissLoading()
        mRemainMoney = money
        tv_money.text = money
    }


    fun setData(t: OrderList) {
        dismissLoading()
        if(page == 1){
            if (t.data.isNullOrEmpty()) {
                mAdapter?.emptyView = empty
            } else {
                mAdapter?.setNewData(t.data)
            }
        }else{
            mAdapter?.addData(t.data)
        }
        tv_money.text = t.totalMoney
        smartRefreshLayout.finishLoadMore()
        smartRefreshLayout.finishRefresh()
    }


}
